<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane v-loading="loading" label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="showDialog=true"
              >新增角色
              </el-button>
            </el-row>
            <!-- 表格 -->
            <el-table border="" :data="list">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template v-slot="{row}">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editRole(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="deleteRole(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                layout="total, prev,pager,next"
                :current-page.sync="page.page"
                :page-size.sync="page.pagesize"
                :total="page.total"
                @current-change="getRoleList"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="companyInfo.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="companyInfo.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="companyInfo.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="companyInfo.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <el-dialog :visible="showDialog" title="编辑角色" @close="btnCancel">
      <el-form ref="formRef" label-width="120px" :model="formData" :rules="rules">
        <el-form-item label="角色" prop="name">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="formData.description" />
        </el-form-item>
      </el-form>

      <template #footer>
        <el-row type="flex" justify="center">
          <el-col :span="12">
            <el-row type="flex" justify="space-between">
              <el-col>
                <el-button type="middle" @click="btnCancel">取消</el-button>
              </el-col>
              <el-col>
                <el-button type="primary" size="middle" @click="btnOk">确定</el-button>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { addRole, deleteRole, getCompanyInfo, getRoleDetail, getRoleList, updateRole } from '@/api/settting'
import { mapGetters } from 'vuex'

export default {
  name: 'RoleList',
  computed: {
    ...mapGetters(['companyId'])
  },
  data() {
    return {
      showDialog: false,
      list: [],
      loading: false,
      page: {
        page: 1,
        pagesize: 2,
        total: 0
      },
      companyInfo: {},
      formData: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          {
            required: true,
            message: '部门名称不能为空'
          }
        ]
      }
    }
  },
  created() {
    this.getRoleList()
    this.getCompanyDetail()
  },
  methods: {
    async editRole(id) {
      console.log(id)
      this.showDialog = true
      this.formData = await getRoleDetail(id)
    },
    btnCancel() {
      this.showDialog = false
      this.$refs.formRef.resetFields()
      this.formData = {
        name: '',
        description: ''
      }
    },
    async btnOk() {
      if (this.formData.id) {
        await updateRole(this.formData)
      } else {
        await addRole(this.formData)
      }

      this.$message.success('提交成功')
      this.btnCancel()
      await this.getRoleList()
    },
    async getCompanyDetail() {
      this.companyInfo = await getCompanyInfo(this.companyId)
    },
    async deleteRole(id) {
      //  提示
      try {
        await this.$confirm('确认删除该角色吗')
        // 只有点击了确定 才能进入到下方
        await deleteRole(id) // 调用删除接口
        this.$message.success('删除角色成功')
        await this.getRoleList() // 重新加载数据
      } catch (error) {
        console.log(error)
      }
    },
    async getRoleList() {
      this.loading = true
      const { rows, total } = await getRoleList(this.page)
      this.list = rows
      this.page.total = total
      this.loading = false
    }
  }
}
</script>
